

package backstage.product;

import java.io.*;

public class productProcess {
private int id;
private String name;
private int price; 
private int stock; 
private byte sale; 
private int typeId;
private int click_count;
private String description;
private File pic;
private File pic_1 ;
private File pic_2  ; 
private File pic_3  ;
 


    
    public productProcess() {
    }
     public String addItem() throws Exception {
        
        NWDB.NWDB mydb=new NWDB.NWDB("javadb","javadb");
        NWDB.IProduct ip=new NWDB.ImplProduct(mydb.getConnection());
        mydb.setIProduct(ip);
        /*  byte[] Pic=null;
             if(getPic()!=null){
                InputStream is = new FileInputStream(getPic());
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte buffer[] = new byte[512];
                int length = 0;
                while( (length = is.read(buffer)) != -1){
            baos.write(buffer, 0, length);
            }
            Pic= baos.toByteArray();
            is.close();
            baos.close();
        }
     */
            byte[] Pic=null;    byte[] Pic_1=null;    byte[] Pic_2=null;    byte[] Pic_3=null;
        
       if(getPic()!=null && getPic_1()!=null && getPic_2()!=null && getPic_3()!=null) 
       {
                InputStream is = new FileInputStream(getPic());            
                InputStream is_1 = new FileInputStream(getPic_1());
                InputStream is_2 = new FileInputStream(getPic_2());
                InputStream is_3 = new FileInputStream(getPic_3());
            
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
              ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
              ByteArrayOutputStream baos3 = new ByteArrayOutputStream();
            byte buffer[] = new byte[512];
            int length = 0;
            while( (length = is.read(buffer)) != -1){
                baos.write(buffer, 0, length);
            }
            Pic= baos.toByteArray();            
            
            while( (length = is_1.read(buffer)) != -1){
                baos1.write(buffer, 0, length);
            }
            Pic_1= baos1.toByteArray();
            
            while( (length = is_2.read(buffer)) != -1){
                baos2.write(buffer, 0, length);
            }
            Pic_2= baos2.toByteArray();
            
            while( (length = is_3.read(buffer)) != -1){
                baos3.write(buffer, 0, length);
            }
            Pic_3= baos3.toByteArray();
            is.close();
            is_1.close();
            is_2.close();
            is_3.close();
            baos.close();
            baos1.close();
            baos2.close();
            baos3.close();
        
     
          mydb.AddProduct(getId(), getName(),  getPrice(), getStock(), getSale(), getTypeId(), getClick_count(),getDescription(),Pic,Pic_1,Pic_2,Pic_3);
      // mydb.AddProduct(id, name, description, price, stock, sale ,  typeId ,Pic, Pic,Pic,Pic);
        mydb.close(); 
        return "success";
       }
       return "again";
       
        
    }
    


    public String updateItem() throws Exception {
        
       NWDB.NWDB mydb=new NWDB.NWDB("javadb","javadb");
        NWDB.IProduct ip=new NWDB.ImplProduct(mydb.getConnection());
        mydb.setIProduct(ip);
          byte[] Pic=null;    byte[] Pic_1=null;    byte[] Pic_2=null;    byte[] Pic_3=null;
        
       if(getPic()!=null && getPic_1()!=null && getPic_2()!=null && getPic_3()!=null) 
          {
                InputStream is = new FileInputStream(getPic());            
                InputStream is_1 = new FileInputStream(getPic_1());
                InputStream is_2 = new FileInputStream(getPic_2());
                InputStream is_3 = new FileInputStream(getPic_3());
            
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
              ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
              ByteArrayOutputStream baos3 = new ByteArrayOutputStream();
            byte buffer[] = new byte[512];
            int length = 0;
            while( (length = is.read(buffer)) != -1){
                baos.write(buffer, 0, length);
            }
            Pic= baos.toByteArray();            
            
            while( (length = is_1.read(buffer)) != -1){
                baos1.write(buffer, 0, length);
            }
            Pic_1= baos1.toByteArray();
            
            while( (length = is_2.read(buffer)) != -1){
                baos2.write(buffer, 0, length);
            }
            Pic_2= baos2.toByteArray();
            
            while( (length = is_3.read(buffer)) != -1){
                baos3.write(buffer, 0, length);
            }
            Pic_3= baos3.toByteArray();
            is.close();
            is_1.close();
            is_2.close();
            is_3.close();
            baos.close();
            baos1.close();
            baos2.close();
            baos3.close();
     
          mydb.UpdateProduct(getId(), getName(),  getPrice(), getStock(), getSale(), getTypeId(), getClick_count(),getDescription(),Pic,Pic_1,Pic_2,Pic_3);
      // mydb.AddProduct(id, name, description, price, stock, sale ,  typeId ,Pic, Pic,Pic,Pic);
                        
          mydb.close();       
        return "success";}
          else{
          mydb.UpdateProduct_noPic(getId(), getName(),  getPrice(), getStock(), getSale(), getTypeId(), getClick_count(),getDescription());
          mydb.close();
          return "updateAgain";}
    }
 
    

    

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

  
    public String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the price
     */
  

    /**
     * @return the stock
     */
    public int getStock() {
        return stock;
    }

    /**
     * @param stock the stock to set
     */
    public void setStock(int stock) {
        this.stock = stock;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    /**
     * @return the sale
     */
    public byte getSale() {
        return sale;
    }

    /**
     * @param sale the sale to set
     */
    public void setSale(byte sale) {
        this.sale = sale;
    }

    /**
     * @return the typeId
     */
    public int getTypeId() {
        return typeId;
    }

    /**
     * @param typeId the typeId to set
     */
    public void setTypeId(int typeId) {
        this.typeId = typeId;
    }

    /**
     * @return the pic
     */
    public File getPic() {
        return pic;
    }

    public void setPic(File pic) {
        this.pic = pic;
    }

    public File getPic_1() {
        return pic_1;
    }

    public void setPic_1(File pic_1) {
        this.pic_1 = pic_1;
    }

    public File getPic_2() {
        return pic_2;
    }

    public void setPic_2(File pic_2) {
        this.pic_2 = pic_2;
    }

    public File getPic_3() {
        return pic_3;
    }

    public void setPic_3(File pic_3) {
        this.pic_3 = pic_3;
    }



    public int getClick_count() {
        return click_count;
    }

    public void setClick_count(int click_count) {
        this.click_count = click_count;
    }

   

}